package com.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.user.mapper.UserMapper;
import com.user.pojo.User;
import com.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        userMapper.insert(user);
    }

    @Override
    public boolean login(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // QueryWrapper<User> userQueryWrapper = Wrappers.query(); 和上面一样的效果
        userQueryWrapper.select("password").eq("username", user.getUsername());
        User selectOne = userMapper.selectOne(userQueryWrapper);
        if (selectOne !=null && selectOne.getPassword().equals(user.getPassword())){
            return true;
        }
        return false;
    }

    @Override
    public User findByName(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        // QueryWrapper<User> userQueryWrapper = Wrappers.query(); 和上面一样的效果
        userQueryWrapper.select("password").eq("username", username);
        return userMapper.selectOne(userQueryWrapper);
    }
}
